<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-width="120px" class="form-container" :inline="true">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="6" >
            <el-form-item  label="二级仓库:"   :required="true"  prop="rid" >
              <el-select v-model="postForm.rid" placeholder="请选择二级仓库">
                <el-option :label="item.roomName" :value="item.id" v-for="item,index of roomListArr" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item  label="dtu号:"   :required="true"  prop="did" >
              <el-select v-model="postForm.did" placeholder="请选择DTU">
                <el-option :label="item.dtuName" :value="item.id" v-for="item,index of dtuListArr" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item  label="设备名称:"   :required="true"  prop="name" >
              <el-input  v-model="postForm.name"  :maxlength="20"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item  label="设备编码:" :required="true"  prop="code"  >
              <el-input v-model="postForm.code"  :maxlength="11" required />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label=" 型  号 :" :required="true"   prop="model">
              <el-input v-model="postForm.model" :maxlength="10"  required  />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item label="特殊资源" :required="true" >
              <el-radio-group v-model="postForm.type">
                <el-radio :label="1" value="1" >单房间W10型</el-radio>
                <el-radio :label="2" value="2" >多房间W40型</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item  label="描  述:" prop="desc"  ><!--接口字段暂时无效-->
              <el-input v-model="postForm.desc" :maxlength="10" name="desc"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="padding-left: 80px">
          <el-col :span="2">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm">提  交</el-button>
          </el-col>
          <el-col :span="2">
            <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
          </el-col>

        </el-row>
      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { userSearch } from '@/api/remoteSearch'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'
import {createUnit, unitDetail} from "../../../api/unit";
import { dtulist }     from "../../../api/dtu"
import { customerList }     from "../../../api/customer"
import { roomList }   from  '@/api/room'
const defaultForm = {
  rid: "",
  did:"",
  name: "",
  code: "",
  model: "",
  type: "1",
  desc: ""
}

export default {
  name: 'UnitAdd',
  components: { Tinymce, MDinput, Upload, Sticky,   CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        // this.$message({
        //   message: rule.field + '为必传项!',
        //   type: 'error'
        // })
        if(rule.field == 'name'){
          callback(new Error('仓库名必填！'))
        }
        if(rule.field == 'did'){
          callback(new Error('did必填！'))
        }
        if(rule.field == 'rid'){
          callback(new Error('rid必填！'))
        }
        if(rule.field == 'model'){
          callback(new Error('model必填！'))
        }
        if(rule.field == 'type'){
          callback(new Error('type必填！'))
        }
        if(rule.field == 'code'){
          callback(new Error('code必填！'))
        }
      } else {
        callback()
      }
    }

    return {
      dtuListArr:[], //dtu列表
      roomListArr: [], //类型数组
      customListArr:[],
      postForm: Object.assign({}, defaultForm),
      loading: false,
      userListOptions: [],
      rules: {
        name :  [{ validator: validateRequire }],
        code :  [{ validator: validateRequire }],
        rid :   [{ validator: validateRequire }],
        did :   [{ validator: validateRequire }],
        type :  [{ validator: validateRequire }],
        model : [{ validator: validateRequire }]
      }
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
    dtulist( {"rid":1,"page":1, "limit":200, "status":0}).then(response => {
      this.dtuListArr = response.data.data
    })
    roomList( {"uissssd":1269,"page":1, "limit":200, "status":0} ).then(response => {
      this.roomListArr = response.data.data
    })
    customerList( {"uissssd":1269,"page":1, "limit":200, "status":0} ).then(response => {
      this.customListArr = response.data.data
    })
  },
  methods: {
    fetchData(id) {
      unitDetail(id).then(response => {
        this.postForm = response.data
        // // Just for test
        // this.postForm.title += `   Article Id:${this.postForm.id}`
        // this.postForm.content_short += `   Article Id:${this.postForm.id}`
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      //this.postForm.display_time = parseInt(this.display_time / 1000)
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
          // this.$notify({
          //   title: '成功',
          //   message: '发布文章成功',
          //   type: 'success',
          //   duration: 2000
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    doSubmitForm() {
      createUnit(this.postForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
          this.resetForm()
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
